import 'package:appflutterhomeschool/model/parent_get_homeworks_model.dart';
import 'package:appflutterhomeschool/provider/provider_widget.dart';
import 'package:appflutterhomeschool/res/colors.dart';
import 'package:appflutterhomeschool/res/styles.dart';
import 'package:appflutterhomeschool/route/application.dart';
import 'package:appflutterhomeschool/route/routes.dart';
import 'package:appflutterhomeschool/ui/widgets/my_app_bar.dart';
import 'package:appflutterhomeschool/ui/widgets/notice/notice_tab_widget.dart';
import 'package:appflutterhomeschool/ui/widgets/widgets.dart';
import 'package:appflutterhomeschool/utils/utils.dart';
import 'package:appflutterhomeschool/view_model/academic_sentiment/task_view_model.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:provider/provider.dart';

///用户： ninetailedfox
///时间： 2020/7/5
///邮箱： 1037438704@qq.com
///功能： 作业任务
class TaskPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return ProviderWidget<TaskViewModel>(
      model: TaskViewModel(context),
      onModelReady: (value) => value.initView(),
      builder: (context, viewModel, child) {
        return Scaffold(
            backgroundColor: Colours.gray_f6,
            appBar: MyAppBar(
                finishBool: false,
                imageRightUrl:
                    viewModel.openScreen ? "shaixuan_true" : "shaixuan_false",
                title: "作业任务",
                onTapLeft: () => Navigator.pop(context),
                fontWeight: FontWeight.bold,
                onTapRight: viewModel.onTapOpenScreen),
            body: viewModel.parentGetHomeworkList == null
                ? Center(
                    child:
                        CircularProgressIndicator(backgroundColor: Colors.blue))
                : viewModel.parentGetHomeworkList.length == 0
                    ? Center(child: Text("暂无数据"))
                    : Column(
                        children: <Widget>[
                          NoticeTabWidget(
                            rightUnread: false,
                            openScreen: viewModel.openScreen,
                            listTab: viewModel.listTab,
                            getIndex: (index) {
                              viewModel.tapModelOnTap(index);
                            },
                          ),
                          Expanded(
                            child: ListView.builder(
                                shrinkWrap: true,
                                itemCount:
                                    viewModel.parentGetHomeworkList.length,
                                itemBuilder: itemList),
                          ),
                        ],
                      ),
            floatingActionButton: Visibility(
              visible: viewModel.identity,
              child: GestureDetector(
                onTap: () => Application().navigatePushToPage(
                    context, Routes.custumNotificationsPage, {"pageTag": 4}),
                child: Image.asset(
                  Utils.getImgPath("xinjian"),
                  width: ScreenUtil().setWidth(103),
                  height: ScreenUtil().setWidth(103),
                ),
              ),
            ));
      },
    );
  }

  Widget itemList(context, index) {
    return Consumer<TaskViewModel>(
      builder: (context, viewModel, child) {
        //判断是否是老师
        bool identity = viewModel.identity;
        ParentGetHomeworksModel parentGetHomeworksModel =
            viewModel.parentGetHomeworkList[index];
        return GestureDetector(
          onTap: () => viewModel.operationDetailsOnTap(index),
          child: Container(
            margin: EdgeInsets.only(
                left: ScreenUtil().setWidth(35),
                right: ScreenUtil().setWidth(35),
                top: ScreenUtil().setWidth(35)),
            decoration: BoxDecoration(
                color: Colors.white, borderRadius: BorderRadius.circular(10)),
            child: Column(
              children: <Widget>[
                Padding(
                  padding: EdgeInsets.only(
                      left: ScreenUtil().setWidth(35),
                      right: ScreenUtil().setWidth(35),
                      top: ScreenUtil().setWidth(35)),
                  child: Row(
                    children: <Widget>[
                      Container(
                        margin: EdgeInsets.only(right: 5),
                        padding: EdgeInsets.only(
                            top: 1, bottom: 1, left: 3, right: 3),
                        decoration: BoxDecoration(
                            color: Colors.blue[50],
                            borderRadius: BorderRadius.circular(3)),
                        child: Text(
                          parentGetHomeworksModel?.subject ?? "科目",
                          style: TextStyles.textBlue30,
                        ),
                      ),
                      Expanded(
                        child: Text(
                          parentGetHomeworksModel?.title ?? "",
                          style: TextStyles.textDarkBold43,
                        ),
                      ),
                      Text(
                        parentGetHomeworksModel
                            .switchStatus(viewModel.identity),
                        style: TextStyles.textF431,
                      )
                    ],
                  ),
                ),
                Container(
                  alignment: Alignment.centerLeft,
                  padding: EdgeInsets.only(
                      left: ScreenUtil().setWidth(35),
                      right: ScreenUtil().setWidth(35),
                      top: ScreenUtil().setWidth(35)),
                  child: identity
                      ? Text("发布于：${parentGetHomeworksModel?.monthDay ?? ""}",
                          style: TextStyles.textGray30)
                      : ItemTopWidget(
                          className: parentGetHomeworksModel?.teacherName ?? "",
                          textContent:
                              parentGetHomeworksModel?.publishTime ?? "",
                          visibility: false,
                        ),
                ),
                Container(
                  alignment: Alignment.centerLeft,
                  padding: EdgeInsets.only(
                      left: ScreenUtil().setWidth(35),
                      right: ScreenUtil().setWidth(35),
                      top: ScreenUtil().setWidth(35)),
                  child: Text(
                    parentGetHomeworksModel?.content ?? "",
                    maxLines: 2,
                    overflow: TextOverflow.ellipsis,
                    style: TextStyles.textContent31,
                  ),
                ),
                parentGetHomeworksModel?.students == null
                    ? Container()
                    : Container(
                        alignment: Alignment.centerLeft,
                        padding: EdgeInsets.only(
                            left: ScreenUtil().setWidth(35),
                            right: ScreenUtil().setWidth(35),
                            top: ScreenUtil().setWidth(55)),
                        child: Text(
                          "布置对象：${parentGetHomeworksModel?.students}",
                          maxLines: 1,
                          overflow: TextOverflow.ellipsis,
                          style: TextStyles.textGray30,
                        ),
                      ),
                Visibility(
                  visible: viewModel.identity,
                  child: Container(
                    alignment: Alignment.centerLeft,
                    padding: EdgeInsets.only(
                        left: ScreenUtil().setWidth(35),
                        right: ScreenUtil().setWidth(35),
                        top: ScreenUtil().setWidth(25)),
                    child: Text.rich(TextSpan(children: [
                      TextSpan(text: "已提交", style: TextStyles.textGray30),
                      TextSpan(
                          text:
                              "${parentGetHomeworksModel?.infosModel?.toCheck}/${parentGetHomeworksModel?.infosModel?.all}",
                          style: TextStyles.textBlue30),
                      TextSpan(text: "已批改", style: TextStyles.textGray30),
                      TextSpan(
                          text:
                              "${parentGetHomeworksModel?.infosModel?.check}/${parentGetHomeworksModel?.infosModel?.all}",
                          style: TextStyles.textBlue30),
                    ])),
                  ),
                ),
                Container(
                  height: 0.3,
                  color: Colors.grey,
                  margin: EdgeInsets.only(top: ScreenUtil().setWidth(35)),
                ),
                Visibility(
                  visible:
                      parentGetHomeworksModel.panduanBool(viewModel.identity),
                  child: Material(
                    color: Colors.white,
                    borderRadius: BorderRadius.only(
                        bottomLeft: Radius.circular(10),
                        bottomRight: Radius.circular(10)),
                    child: InkWell(
                      borderRadius: BorderRadius.only(
                          bottomLeft: Radius.circular(10),
                          bottomRight: Radius.circular(10)),
                      child: Container(
                        height: ScreenUtil().setWidth(123),
                        decoration: BoxDecoration(
                          borderRadius: BorderRadius.only(
                              bottomLeft: Radius.circular(10),
                              bottomRight: Radius.circular(10)),
                        ),
                        alignment: Alignment.center,
                        child: Text(
                          parentGetHomeworksModel
                              .buttonStatus(viewModel.identity),
                          style: TextStyles.textBlue37,
                        ),
                      ),
                      onTap: (){
                        if(viewModel.identity){
                          viewModel.correctingOnTap(index);
                        }else{
                          viewModel.operationDetailsOnTap(index);
                        }
                      },
                    ),
                  ),
                )
              ],
            ),
          ),
        );
      },
    );
  }
}
